package com.wanhe.shoppingcart.cuiyu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.wanhe.shoppingcart.R;
import com.wanhe.shoppingcart.bean.CartShowItem;
import com.wanhe.shoppingcart.CartChildGridAdapter;
import com.wanhe.shoppingcart.manager.ICartItem;

import java.util.ArrayList;


/**
 * Created by XieZaiWei on 2017/5/22.
 * 大丰收脆鱼购物车Adapter
 */

public class CuiYuCartAdapter extends BaseExpandableListAdapter {
    private static final int TYPE_LIST = 0;
    private static final int TYPE_GRID = 1;
    private static final int TYPE_SET_MEAL = 2;
    protected Context mContext;
    protected LayoutInflater mLayoutInflater;
    protected ArrayList<CartShowItem> mArrCartShowItem;
    private int mGroupLayout;
    private int mGridItemLayout;
    private int mListItemLayout;
    private int mSetMealItemLayout;

    public CuiYuCartAdapter(Context context, int groupLayout, int gridItemLayout, int listItemLayout, int setMealItemLayout) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mGroupLayout = groupLayout;
        mGridItemLayout = gridItemLayout;
        mListItemLayout = listItemLayout;
        mSetMealItemLayout = setMealItemLayout;
    }

    public void setData(ArrayList<CartShowItem> arrCartShowItem) {
        if(mArrCartShowItem != null){
            mArrCartShowItem.clear();
            mArrCartShowItem = null;
        }
        mArrCartShowItem = arrCartShowItem;
        notifyDataSetChanged();
    }

    public void clean(){
        if(mArrCartShowItem != null){
            mArrCartShowItem.clear();
            mArrCartShowItem = null;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        if(mArrCartShowItem == null){
            return 0;
        }
        return mArrCartShowItem.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public CartShowItem getGroup(int groupPosition) {
        return mArrCartShowItem.get(groupPosition);
    }

    @Override
    public ICartItem getChild(int groupPosition, int childPosition) {
        return mArrCartShowItem.get(groupPosition).getArrCartItem().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder;
        if (convertView == null) {
            holder = new GroupHolder();
            convertView = mLayoutInflater.inflate(mGroupLayout, null);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);

        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.tv_title.setText(mArrCartShowItem.get(groupPosition).getTitle());

        return convertView;
    }

    @Override
    public int getChildTypeCount() {
        return 3;
    }

    @Override
    public int getChildType(int groupPosition, int childPosition) {
        CartShowItem cartShowItem = mArrCartShowItem.get(groupPosition);
        int type;
        switch (cartShowItem.getTitle()) {
            case "双拼鱼锅":
            case "金椒翡翠":
                type =  TYPE_LIST;
                break;
            case "套餐":
                type =  TYPE_SET_MEAL;
                break;

            default:
                type =  TYPE_GRID;
                break;
        }
        return type;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        int type = getChildType(groupPosition, childPosition);
        GridHolder gridHolder = null;
        ListHolder listHolder = null;
        if (convertView == null) {
            switch (type) {
                case TYPE_LIST:
                case TYPE_SET_MEAL:
                    listHolder = new ListHolder();
                    convertView = mLayoutInflater.inflate(R.layout.cart_child_list, null);
                    listHolder.lv_dish = (ListView) convertView.findViewById(R.id.lv_dish);
                    convertView.setTag(listHolder);

                    break;

                case TYPE_GRID:
                    gridHolder = new GridHolder();
                    convertView = mLayoutInflater.inflate(R.layout.cart_child_grid, null);
                    gridHolder.gv_dish = (GridView) convertView.findViewById(R.id.gv_dish);
                    convertView.setTag(gridHolder);
                    break;

                default:
                    break;
            }


        }

        switch (type) {
            case TYPE_LIST:
                listHolder = (ListHolder) convertView.getTag();
                listHolder.lv_dish.setAdapter(new CuiYuListAdapter(mContext, mArrCartShowItem.get(groupPosition).getArrCartItem(), mListItemLayout));
                break;

            case TYPE_SET_MEAL:
                listHolder = (ListHolder) convertView.getTag();
                listHolder.lv_dish.setAdapter(new CuiYuSetMealAdapter(mContext, mArrCartShowItem.get(groupPosition).getArrCartItem(), mSetMealItemLayout));
                break;

            case TYPE_GRID:
                gridHolder = (GridHolder) convertView.getTag();
                gridHolder.gv_dish.setAdapter(new CartChildGridAdapter(mContext, mArrCartShowItem.get(groupPosition).getArrCartItem(), mGridItemLayout));
                break;

            default:
                break;
        }


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    protected static class GroupHolder {
        public TextView tv_title;
    }

    protected static class GridHolder {
        public GridView gv_dish;
    }

    protected static class ListHolder {
        public ListView lv_dish;
    }
}
